/*
 * @Author: zzr
 * @Date: 2025-03-03 11:11:41
 * @LastEditors: zzr
 * @Description:
 */
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// * Copyright 2023 The Geek-AI Authors. All rights reserved.
// * Use of this source code is governed by a Apache-2.0 license
// * that can be found in the LICENSE file.
// * @Author yangjian102621@163.com
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

import { createRouter, createWebHistory } from "vue-router";
import { getUserToken, removeUserToken } from "./store/session";
import { useUserStoreWithOut } from "@/store/user";
import { useDictStoreWithOut } from "@/store/dict";
import { getInfo } from "@/api/login";
import { getMenu } from "@/api/menu";
import { ElMessage } from "element-plus";
const routes = [
  // {
  //   name: "Index",
  //   path: "/",
  //   meta: { title: "首页" },
  //   component: () => import("@/views/Index.vue"),
  // },
  {
    name: "home",
    path: "/",
    redirect: "/index",
    component: () => import("@/views/Home.vue"),
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
      {
        name: "Index",
        path: "/",
        meta: { title: "首页" },
        component: () => import("@/views/Welcome.vue"),
      },
      // {
      //   name: "chat",
      //   path: "/chat",
      //   meta: { title: "创作中心" },
      //   component: () => import("@/views/ChatPlus.vue"),
      // },
      {
        name: "chat",
        path: "/chat",
        meta: { title: "创作中心" },
        component: () => import("@/views/chat/index/index.vue"),
      },
      {
        name: "deepSeek",
        path: "/deepSeek",
        meta: { title: "deepSeek" },
        component: () => import("@/views/chat/index/index.vue"),
      },
      {
        name: "image-mj",
        path: "/mj",
        meta: { title: "AI 绘画中心" },
        component: () => import("@/views/ImageMj.vue"),
      },
      {
        name: "audit",
        path: "/audit",
        meta: { title: "AI 内容审核" },
        component: () => import("@/views/audit.vue"),
      },
       {
        name: "ppt",
        path: "/ppt",
        meta: { title: "AI PPT" },
        component: () => import("@/views/ppt/index.vue"),
      },
      {
        name: "image-sd",
        path: "/sd",
        meta: { title: "stable diffusion 绘画中心" },
        component: () => import("@/views/ImageSd.vue"),
      },
      {
        name: "member",
        path: "/member",
        meta: { title: "会员充值中心" },
        component: () => import("@/views/Member.vue"),
      },
      {
        name: "chat-app",
        path: "/apps",
        meta: { title: "应用中心" },
        component: () => import("@/views/ChatApps.vue"),
      },
      {
        name: "images",
        path: "/images-wall",
        meta: { title: "作品展示" },
        component: () => import("@/views/ImagesWall.vue"),
      },
      {
        name: "user-invitation",
        path: "/invite",
        meta: { title: "推广计划" },
        component: () => import("@/views/Invitation.vue"),
      },
      {
        name: "powerLog",
        path: "/powerLog",
        meta: { title: "消费日志" },
        component: () => import("@/views/PowerLog.vue"),
      },
      {
        name: "xmind",
        path: "/xmind",
        meta: { title: "思维导图" },
        component: () => import("@/views/MarkMap.vue"),
      },
      {
        name: "dalle",
        path: "/dalle",
        meta: { title: "DALLE-3" },
        component: () => import("@/views/Dalle.vue"),
      },
      {
        name: "suno",
        path: "/aiMusic",
        meta: { title: "AI音乐" },
        component: () => import("@/views/Suno.vue"),
      },
      {
        name: "video-kling",
        path: "/video",
        meta: { title: "AI视频" },
        component: () => import("@/views/VideoKling.vue"),
      },
      {
        name: "siweidaotu",
        path: "/siweidaotu",
        meta: { title: "AI视频" },
        component: () => import("@/views/mindmap/index/index.vue"),
      },
      {
        name: "video-kling",
        path: "/video",
        meta: { title: "AI视频" },
        component: () => import("@/views/VideoKling.vue"),
      },
      {
        name: "videoModel",
        path: "/videoModel/:model",
        meta: { title: "AI视频", activePath: "/video" },
        component: () => import("@/views/VideoKlingModel.vue"),
      },
      {
        name: "ExternalLink",
        path: "/external",
        component: () => import("@/views/ExternalPage.vue"),
      },
      {
        name: "song",
        path: "/song/:id",
        meta: { title: "Suno音乐播放" },
        component: () => import("@/views/Song.vue"),
      },
    ],
  },
  {
    name: "chat-export",
    path: "/chat/export",
    meta: { title: "导出会话记录" },
    component: () => import("@/views/ChatExport.vue"),
  },
  {
    name: "login",
    path: "/login",
    meta: { title: "用户登录" },
    component: () => import("@/views/Login.vue"),
  },
  {
    name: "login-callback",
    path: "/login/callback",
    meta: { title: "用户登录" },
    component: () => import("@/views/LoginCallback.vue"),
  },
  {
    name: "register",
    path: "/register",

    meta: { title: "用户注册" },
    component: () => import("@/views/Register.vue"),
  },

  // {
  //   name: "mobile",
  //   path: "/mobile",
  //   meta: { title: "Geek-AI v4.0" },
  //   component: () => import("@/views/mobile/Home.vue"),
  //   redirect: "/mobile/index",
  //   children: [
  //     {
  //       path: "/mobile/index",
  //       name: "mobile-index",
  //       component: () => import("@/views/mobile/Index.vue"),
  //     },
  //     {
  //       path: "/mobile/chat",
  //       name: "mobile-chat",
  //       component: () => import("@/views/mobile/ChatList.vue"),
  //     },
  //     {
  //       path: "/mobile/image",
  //       name: "mobile-image",
  //       component: () => import("@/views/mobile/Image.vue"),
  //     },
  //     {
  //       path: "/mobile/profile",
  //       name: "mobile-profile",
  //       component: () => import("@/views/mobile/Profile.vue"),
  //     },
  //     {
  //       path: "/mobile/imgWall",
  //       name: "mobile-img-wall",
  //       component: () => import("@/views/mobile/pages/ImgWall.vue"),
  //     },
  //     {
  //       path: "/mobile/chat/session",
  //       name: "mobile-chat-session",
  //       component: () => import("@/views/mobile/ChatSession.vue"),
  //     },
  //     {
  //       path: "/mobile/chat/export",
  //       name: "mobile-chat-export",
  //       component: () => import("@/views/mobile/ChatExport.vue"),
  //     },
  //   ],
  // },

  {
    name: "test",
    path: "/test",
    meta: { title: "测试页面" },
    component: () => import("@/views/Test.vue"),
  },
  {
    name: "NotFound",
    path: "/:all(.*)",
    meta: { title: "页面没有找到" },
    component: () => import("@/views/404.vue"),
  },
];

// console.log(MY_VARIABLE)
const router = createRouter({
  history: createWebHistory(),
  routes: routes,
});

let prevRoute = null;
// const whiteList = ["/login", "/pdf/viewer"];
// dynamic change the title when router change
router.beforeEach(async (to, from, next) => {
  document.title = getUserToken() ? to.meta.title : "首页";
  const userStore = useUserStoreWithOut();
  const dictStore = useDictStoreWithOut();
  prevRoute = from;
  if (!userStore.menu) {
    console.log(
      "%c获取菜单",
      "color: white; background-color: #4facfe; padding: 2px 5px; border-radius: 2px"
    );
    try {
      let menu = await getMenu();
      userStore.setMenu(menu);
    } catch (error) {
      next();
    }
  }

  if (getUserToken()) {
    // 获取所有字典
    if (!dictStore.getIsSetDict) {
      await dictStore.setDictMap();
    }
    if (!userStore.user) {
      try {
        console.log(
          "%c获取info",
          "color: white; background-color: #4facfe; padding: 2px 5px; border-radius: 2px"
        );
        let info = await getInfo();
        userStore.setUser(info);
        next();
      } catch (error) {
        removeUserToken();
        next("/");
      }
    } else {
      next();
    }
  } else {
    if (to.path == "/") {
      next();
    } else {
      removeUserToken();
      ElMessage({
        message: "请先登录",
        type: "error",
      });
      next({
        path: "/",
      });
    }
  }
  // const hasToken = getUserToken();

  // if (hasToken) {
  //   if (to.path === "/login") {
  //     // if is logged in, redirect to the home page
  //     next({ path: "/" });
  //   } else {
  //     // determine whether the user has obtained his permission roles through getInfo
  //     next();
  //   }
  // } else {
  //   if (whiteList.indexOf(to.path) !== -1) {
  //     // in the free login whitelist, go directly
  //     next();
  //   } else {
  //     // other pages that do not have permission to access are redirected to the login page.
  //     next(`/login`); // ?redirect=${to.path}
  //   }
  // }
});

export { router, prevRoute };
